package com.ruoyi.wms.controller;

import java.util.List;

import com.ruoyi.wms.domain.vo.MailboxAddressTreeSelectVo;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.idempotent.annotation.RepeatSubmit;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.web.core.BaseController;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.wms.domain.vo.MailboxAddressVo;
import com.ruoyi.wms.domain.bo.MailboxAddressBo;
import com.ruoyi.wms.service.MailboxAddressService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

/**
 * 填写地配置
 *
 * @author zcc
 * @date 2024-12-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wms/address")
public class MailboxAddressController extends BaseController {



    private final MailboxAddressService mailboxAddressService;
    /**
     * 查询填写地列表
     */
    @SaCheckPermission("wms:address:list")
    @GetMapping("/list")
    public TableDataInfo<MailboxAddressVo> list(MailboxAddressBo bo, PageQuery pageQuery) {
        return mailboxAddressService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询填写地列表
     */
    @SaCheckPermission("wms:address:list")
    @GetMapping("/listNoPage")
    public R<List<MailboxAddressVo>> listNoPage(MailboxAddressBo bo) {
        return R.ok(mailboxAddressService.queryList(bo));
    }


    /**
     * 获取填写地类型下拉树列表
     */
    @GetMapping("/treeselect")
    @SaCheckPermission("wms:item:treelist")
    public R<List<MailboxAddressTreeSelectVo>> treeselect(MailboxAddressBo query) {
        List<MailboxAddressVo> itemTypes = mailboxAddressService.queryList(query);
        return R.ok(mailboxAddressService.buildItemTypeTreeSelect(itemTypes));
    }
    /**
     * 导出填写地列表
     */
    @SaCheckPermission("wms:address:list")
    @Log(title = "填写地", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MailboxAddressBo bo, HttpServletResponse response) {
        List<MailboxAddressVo> list = mailboxAddressService.queryList(bo);
        ExcelUtil.exportExcel(list, "填写地", MailboxAddressVo.class, response);
    }

    /**
     * 获取填写地详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("wms:address:list")
    @GetMapping("/{id}")
    public R<MailboxAddressVo> getInfo(@NotNull(message = "主键不能为空")
                                  @PathVariable Long id) {
        return R.ok(mailboxAddressService.queryById(id));
    }

    /**
     * 新增填写地
     */
    @SaCheckPermission("wms:address:edit")
    @Log(title = "填写地", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MailboxAddressBo bo) {
        mailboxAddressService.insertByBo(bo);
        return R.ok();
    }

    /**
     * 修改填写地
     */
    @SaCheckPermission("wms:address:edit")
    @Log(title = "填写地", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MailboxAddressBo bo) {
        mailboxAddressService.updateByBo(bo);
        return R.ok();
    }

    /**
     * 删除填写地
     *
     * @param id 主键
     */
    @SaCheckPermission("wms:address:edit")
    @Log(title = "填写地", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@NotNull(message = "主键不能为空")
                          @PathVariable Long id) {
        mailboxAddressService.deleteById(id);
        return R.ok();
    }

    @SaCheckPermission("wms:address:edit")
    @Log(title = "填写地", businessType = BusinessType.UPDATE)
    @PostMapping("/update/orderNum")
    public R<Void> updateOrderNum(@RequestBody List<MailboxAddressBo> tree) {
        mailboxAddressService.updateOrderNum(tree);
        return R.ok();
    }
}
